Ext.onReady(function(){
    var LettersForm = Ext.create('Ext.form.Panel', {
        plain: true,
        border: 0,
        bodyPadding: 5,
        fieldDefaults: {
        	labelAlign: 'right',
            labelWidth: 60,
            anchor: '100%'
        },
        layout: {
            type: 'vbox',
            align: 'stretch'
        },
        items: [{
			xtype:'hiddenfield',
			fieldLabel:'receiverIds',
			name:'receiverIds',
			allowBlank: false
		},{
            xtype: 'textfield',
            fieldLabel: '收件人',
            readOnly: true,
            name: 'receiver',
            allowBlank: false
        },{
            xtype: 'textfield',
            fieldLabel: '主题',
            name: 'title'
        },{
            xtype: 'htmleditor',
            fieldLabel: '正文',
            name: 'content',
            flex:1
        },{
            xtype: 'fieldcontainer',
            id:'attachmentContainer',
            height: 60,
            autoScroll: true,
            layout: {
                type: 'anchor'
            },
            anchor: '100%',
            fieldLabel: '附件'
        },{
			xtype:'hiddenfield',
			fieldLabel:'attachments',
			name:'attachments'
		}],            
        dockedItems: [{
            xtype: 'toolbar',
            dock: 'top',
            items: [{
                    xtype: 'button',
                    text: '发送',
					handler : function() {
						var containers=Ext.getCmp('attachmentContainer').query('container');
						var attachmentArr=[];
	        			Ext.each(containers, function (c) {
	        				attachmentArr.push(c.id+':'+c.name);
	        			});	
	        			LettersForm.getForm().findField('attachments').setValue(attachmentArr.join(';'));
	        			
						if (LettersForm.getForm().isValid()) {
							LettersForm.getForm().submit({
								submitEmptyText:false,
								waitTitle : '请稍候',
								waitMsg : '正在处理......',
								url : '/resourcecontrol/letters_save/',
								success : function(form, action) {
									LettersForm.getForm().reset();
									Ext.getCmp('attachmentContainer').removeAll();
								},
								failure : function(form, action) {
									if (action.result) {
										Ext.Msg.alert('系统提示', action.result.message);
									} 
								}
							});
						}
					}                    
                },
//                {
//                    xtype: 'button',
//                    text: '定时发送',
//                    handler:function(){
//                    	
//                    }
//                },
                {
                    xtype: 'button',
                    text: '通讯录',
                    handler : function(){
						var AddressBookTree=Ext.create('Ext.tree.Panel',{
							width: 350,
							height:300,
							store: Ext.create('Ext.data.TreeStore', {
								proxy: {
									type: 'ajax',
									url: '/employecontrol/employe_tree/',
							        actionMethods: {
							            read: 'POST'
							        }
								},
								autoLoad: true
							}),
					        rootVisible: false,
							split:true,
							listeners : {  
						        'checkchange' : function(node,check){
								    node.cascadeBy(function(child){
								        child.set("checked",check);
								    });
						        }  
						    },
							dockedItems:[{
								xtype:'toolbar',
								items:[{
				                    xtype: 'radiogroup',
				                    width: 120,
				                    items: [
				                        {
				                            xtype: 'radiofield',
				                            name:'addressBookOrder',
				                            boxLabel: '按校区',
				                            inputValue: '0',
				                            checked:true,
				                            listeners:{
				                            	change:function( me, newValue, oldValue, eOpts ){
				                            		var params = {sort:1};
				                            		if(newValue)
									        			params = {sort:0};
								        		    Ext.apply(AddressBookTree.getStore().proxy.extraParams, params);
								        		    AddressBookTree.getStore().load();					                            		
				                            	}
				                            }
				                        },
				                        {
				                            xtype: 'radiofield',
				                            name:'addressBookOrder',
				                            boxLabel: '按部门',
				                            inputValue: '1'
				                        }
				                    ]
				                }]
							}]
						});
						
						var AddressBookWindow = Ext.create('Ext.window.Window', {
							title: '通讯录',
							modal: true,
							closeAction: 'hide',
							items : [AddressBookTree],
							resizable : false,
							buttonAlign : 'center',
							buttons : [ {
								text : '确认',
								handler : function() {
									var receiverIdField=LettersForm.getForm().findField('receiverIds');
									var receiverField=LettersForm.getForm().findField('receiver');
									var records = AddressBookTree.getView().getChecked();
									var ids=[];
									var names=[];
									var oldIds=receiverIdField.getValue().split(";");
									var oldNames=receiverField.getValue().split(";");
				                    Ext.Array.each(records, function(rec){
				                    	if(rec.get('leaf')){
			                    			var flag=false;
				                    		Ext.Array.each(oldIds,function(oldId){
				                    			if(oldId==rec.get('id')){
				                    				flag=true;
				                    				return;
				                    			}
				                    		});
				                    		if(!flag){
			                    				ids.push(rec.get('id'));
												names.push(rec.get('text'));
				                    		}
				                    	}
				                    });
				                    receiverIdField.setValue(ids.concat(oldIds).join(';'));
				                    receiverField.setValue(names.concat(oldNames).join(';'));
				                    AddressBookWindow.hide();
								}
							}, {
								text : '取消',
								handler : function() {
									AddressBookWindow.hide();
								}
							}]
						});
						AddressBookWindow.show();
                    }
                },{
                	xtype:'filefield',
                	buttonText:'上传',
                	buttonOnly: true,
                	name:'file',
		            listeners:{
		            	change:function(me,value,eOpts){
		            		var hasChild=Ext.getCmp('attachmentContainer').getChildByElement(value);
		            		if(hasChild){
		            			Ext.Msg.alert('系统提示', '附件已上传!');
		            			return ;
		            		}

		            		fileForm=Ext.create('Ext.form.Panel');
		            		fileForm.add(me);
							fileForm.getForm().submit({
								submitEmptyText:false,
								waitTitle : '请稍候',
								waitMsg : '正在处理......',
								url : '/uploaded/',
								success : function(form, action) {
									if (action.result) {
										Ext.Msg.alert('系统提示', action.result.message);
										
					            		var ChildContainer=Ext.create('Ext.container.Container',{
					            			id:value,
					            			name:action.result.filename,
								            items: [{
							                    xtype: 'label',
							                    text: value
							                },{
							                    xtype: 'button',
							                    text: '删除',
							                    handler:function(){
							                    	Ext.getCmp('attachmentContainer').remove(value);
							                    }
							            	}]		            			
					            		});
										Ext.getCmp('attachmentContainer').add(ChildContainer);	
									} 
								},
								failure : function(form, action) {
									if (action.result) {
										Ext.Msg.alert('系统提示', action.result.message);
									} 
								}
							});
		            	}
		            }                	
                }
            ]
        }]
    });
	
	
	
	Ext.create('Ext.container.Viewport',{
		layout:'fit',
		renderTo: 'lettersform-panel',
		items:[LettersForm]
	});	
});